<?php
/**
 * 《题库管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */

require_once(dirname(__FILE__) . '/ExamUtil.class.php');

class Question extends ExamUtil {
	public $tbl = Constant::tbl_question;

	public $category = array(
		'-' => '请选择',
		'公共题' => '公共题',
		'产品设计' => '产品设计',
		
		'采购' => '采购',
		'文员' => '文员',		
		'英语' => '英语',
	
		'财务' => '财务',
		'司机' => '司机',
		'其它' => '其它',
	);	
	
	public $status = array(
		'-' => '请选择',
		'1' => '有效',
		'2' => '无效',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'type' => '类型',
		'status' => '状态',

		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		'supplier' => '供应商',
		'emp' => '工程师',
		'currency' => '币种',
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($c) {
		parent::pretreat($c);
		
		$c->assign_by_ref('type_option', $this->type);
		$c->assign_by_ref('category_option', $this->category);
	}
	
	/**
	 * 首页
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function tab($controller, $request) {		
		$data = array(
			'试卷管理' => array('cc'=>'exam.php?do=Exam.find', 'js'=>''),
			'试卷题目' => array('cc'=>'exam.php?do=ExamQuestion.find', 'js'=>''),
			'考试人员' => array('cc'=>'exam.php?do=ExamEmp.find', 'js'=>''),
		
			// '答题情况' => array('cc'=>'exam.php?do=Answer.find', 'js'=>''),
				
			'题库信息' => array('cc'=>'exam.php?do=Question.find', 'js'=>''),
			// '公共笔试' => array('cc'=>'exam.php?do=Exam.pub', 'js'=>''),
		    // '题库选项' => array('cc'=>'exam.php?do=QuestionItem.find', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-7 day'));
		
		$controller->display($request, 'exam/question/find');
		return true;
	}
	
	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'mobile') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['mobile'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {				
		$category_s = $request['category_s'];		
		$type_s = $request['type_s'];
		$name_s = $request['name_s'];
		
		$oper_s = $this->_input($request['oper_s']);
				
		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}		
				
		if (!empty($category_s)) {
			$where .= " AND category LIKE '" . $category_s . "%'";
		}
				
		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}
		
		if (!empty($name_s)) {
			$where .= " AND name LIKE '%" . $name_s . "%'";
		}
						
		if (!empty($oper_s)) {
			$where .= " AND oper = '" . $oper_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}

		if ($this->_audit('148')) {
			return '';
		}

		$emp = $_SESSION['id'];

		return " oper = '" . $emp . "'";
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'exam/question/list');
		return true;
	}
	
	function _num($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(*) t FROM %s WHERE mold = '%s'";
		$sql = sprintf($fmt, Constant::tbl_cw_mold_log, $id);
		// debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		return $row['t'];
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-3 day'));

		$controller->display($request, 'exam/question/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);
		
		$category = addslashes($request['category']);
		$name = addslashes($request['name']);
		$item = addslashes($request['item']);
		$type = $request['type'];
		$score = $request['score'];
		$answer = addslashes($request['answer']);
		$content = addslashes($request['content']);
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(category, name, item, type, score, answer, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $category, $name, $item, $type, $score, $answer, $content, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _insert_mold_log($id, $log, $emp) {
		require_once(dirname(__FILE__) . '/MoldLog.class.php');
		$aa = new MoldLog();
		
		return $aa->_insert($id, $log, $emp);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);
		
		if ($row['type'] < 4) {
			$a = preg_split("/[\n,]+/", trim($row['item']));
			$answer = $row['answer'];
			
			$b = array();
			$count = count($a);
			for ($i = 0; $i < $count; $i++) {
				$ss = strval($i);
				if (strpos($answer, $ss) === false) {
					$b[] = array('v'=>$a[$i], 'c'=>'');;
				} else {
					$b[] = array('v'=>$a[$i], 'c'=>'checked');;
				}
			}			
			// debug(print_r($b, true));

			$row['item_t'] = $b;
		}
		
		$row['type_t'] = $this->type[$row['type']];

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'exam/question/edit');
		return true;
	}
	
	function _mold_log($id, $status = 'all') {
		require_once(dirname(__FILE__) . '/MoldLog.class.php');
		$aa = new MoldLog();
		
		return $aa->_list($id);		
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);
				
		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		
		$category = addslashes($request['category']);
		$item = addslashes($request['item']);
		$name = addslashes($request['name']);
		$score = $request['score'];
		$answer = addslashes($request['answer']);
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET category = '%s', name = '%s', item = '%s', score = '%s', answer = '%s', content = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $category, $name, $item, $score, $answer, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}	

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _update_mold_log($id, $log, $emp) {
		require_once(dirname(__FILE__) . '/MoldLog.class.php');
		$aa = new MoldLog();
		
		return $aa->_update($id, $log, $emp);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$row['type_t'] = $this->type[$row['type']];
		$row['oper_t'] = Name::emp($row['oper']);	
		$row['status_t'] = $this->status[$row['status']];
		
		$row['answer'] = $this->_ab($row['answer']);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'exam/question/view');
		return true;
	}
	
	function _list($controller, $id) {
		$db = newdb($this->dsn_r);
		$data = array();
		$fmt = "SELECT * FROM %s WHERE status = '1' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl);
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$row['type_t'] = $this->type[$row['type']];
			
			if ($this->_question_exam($row['id'], $id)) {
				$row['class'] = "class='added'";
				$data[] = $row;
			}

			$data_all[] = $row;
		}
		
		$controller->assign_by_ref('question', $data);
		$controller->assign_by_ref('question_all', $data_all);
	}
	
	function _question_exam($question, $exam) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '1' AND question = '%s' AND exam = '%s'";
		$sql = sprintf($fmt, Constant::tbl_exam_question, $question, $exam);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($row['status'] == '1') {
				return true;
			}
		}

		return false;
	}

	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
				
		$row = $this->_view_t($row);

		return $row;
	}
	
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->id($this->tbl, $id);
			
			if ($this->_is_delete($row)) {
				$fmt = "DELETE FROM %s WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $id);
				// debug($sql);
				$db->query($sql);
			}
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['status'] > 1) {
			return false;
		}

		if ($this->_audit('77')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
		
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));

		$num = array(
			'1' => '按次数',
			'2' => '按金额',
		);
		$controller->assign('num_option', $num);

		$controller->display($request, 'finance/mold/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function groupby_dd($px) {
		if ($px == '1') {
			return "DATE_FORMAT(begin, '%Y-%m-%d %H')";
		}

		if ($px == '2') {
			return "DATE_FORMAT(begin, '%Y-%m-%d')";
		}

		if ($px == '3') {
			return "DATE_FORMAT(begin, '%Y-%m')";
		}

		return $px;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(total) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(total) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);

		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp' || $px == 'driver') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}
}
?>